package z_gawibaibo;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

import javax.swing.ScrollPaneConstants;
import javax.swing.ImageIcon;

public class GawuBaiBo extends JFrame {

	boolean gameStart = false;
	int computer = -1; // 0=가위, 1=바위, 2=보
	
	int gCnt=0;
	int wCnt=0;
	int fCnt=0;
	int sCnt=0;
	
	
	private JPanel contentPane;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea message;
	private JLabel lblNewLabel;
	private JTextField gameCnt;
	private JLabel lblNewLabel_1;
	private JTextField winCnt;
	private JTextField failCnt;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JButton btnNewButton_2;
	private JButton btnNewButton_3;
	private JLabel computerSu;
	private JTextField safeCnt;
	private JLabel lblNewLabel_5;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GawuBaiBo frame = new GawuBaiBo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GawuBaiBo() {
		setTitle("가위바위보 게임");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getScrollPane());
		contentPane.add(getLblNewLabel());
		contentPane.add(getGameCnt());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getWinCnt());
		contentPane.add(getFailCnt());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getLblNewLabel_3());
		contentPane.add(getBtnNewButton_2());
		contentPane.add(getBtnNewButton_3());
		contentPane.add(getComputerSu());
		contentPane.add(getSafeCnt());
		contentPane.add(getLblNewLabel_5());
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("가위");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(gameStart){
						check(0);
					}else{
						message.append("게임시작 버튼을 먼저 클릭해 주세요\n\n");
					}
				}
			});
			btnNewButton.setBounds(223, 261, 65, 41);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("시작");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(gameStart){
						gameStart = false;
						btnNewButton_1.setText("시작");
						message.append("게임이 종료되었습니다.\n\n");
					}else{
						gameStart = true;
						btnNewButton_1.setText("종료");

						gameCnt.setText("0");
						winCnt.setText("0");
						failCnt.setText("0");
						safeCnt.setText("0");
						
						gCnt=0;
						wCnt=0;
						fCnt=0;
						sCnt=0;

						message.append("게임이 시작되었습니다.\n");
						message.append("가위, 바위, 보 버튼중 하나를 선택하세요...\n\n");
					}
					message.setCaretPosition(message.getText().length());
				}
			});
			btnNewButton_1.setBounds(325, 10, 97, 95);
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			scrollPane.setBounds(12, 10, 157, 292);
			scrollPane.setViewportView(getMessage());
		}
		return scrollPane;
	}
	private JTextArea getMessage() {
		if (message == null) {
			message = new JTextArea();
			message.setLineWrap(true);
			message.setWrapStyleWord(true);
		}
		return message;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("승률");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(181, 14, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getGameCnt() {
		if (gameCnt == null) {
			gameCnt = new JTextField();
			gameCnt.setText("0");
			gameCnt.setHorizontalAlignment(SwingConstants.CENTER);
			gameCnt.setBounds(181, 35, 65, 21);
			gameCnt.setColumns(10);
		}
		return gameCnt;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("전");
			lblNewLabel_1.setBounds(251, 38, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getWinCnt() {
		if (winCnt == null) {
			winCnt = new JTextField();
			winCnt.setText("0");
			winCnt.setHorizontalAlignment(SwingConstants.CENTER);
			winCnt.setBounds(181, 59, 65, 21);
			winCnt.setColumns(10);
		}
		return winCnt;
	}
	private JTextField getFailCnt() {
		if (failCnt == null) {
			failCnt = new JTextField();
			failCnt.setText("0");
			failCnt.setHorizontalAlignment(SwingConstants.CENTER);
			failCnt.setBounds(181, 84, 65, 21);
			failCnt.setColumns(10);
		}
		return failCnt;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("승");
			lblNewLabel_2.setBounds(251, 63, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("패");
			lblNewLabel_3.setBounds(251, 87, 57, 15);
		}
		return lblNewLabel_3;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("바위");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(gameStart){
						check(1);
					}else{
						message.append("게임시작 버튼을 먼저 클릭해 주세요\n\n");
						message.setCaretPosition(message.getText().length());
					}
				}
			});
			btnNewButton_2.setBounds(291, 261, 65, 41);
		}
		return btnNewButton_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("보");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(gameStart){
						check(2);
					}else{
						message.append("게임시작 버튼을 먼저 클릭해 주세요\n\n");
						message.setCaretPosition(message.getText().length());
					}
				}
			});
			btnNewButton_3.setBounds(357, 261, 65, 41);
		}
		return btnNewButton_3;
	}
	private JLabel getComputerSu() {
		if (computerSu == null) {
			computerSu = new JLabel("");
			computerSu.setIcon(new ImageIcon("ga.png"));
			computerSu.setFont(new Font("굴림", Font.PLAIN, 37));
			computerSu.setBackground(Color.LIGHT_GRAY);
			computerSu.setHorizontalAlignment(SwingConstants.CENTER);
			computerSu.setBounds(275, 115, 147, 131);
		}
		return computerSu;
	}
	
	public void check(int human){
		gCnt++;
		String temp = "Computer WIN !!!!";

		computer = new Random().nextInt(3);

		if(computer == 0) computerSu.setIcon(new ImageIcon("ga.png"));
		else if(computer == 1) computerSu.setIcon(new ImageIcon("ba.png"));
		else 			computerSu.setIcon(new ImageIcon("bo.png"));

		
		
		if(computer == human){ // 비김
			sCnt++;
			temp = "비겼다 !!!";
		}else{
			if(computer == 0 && human == 2){//컴승
				fCnt++;
			}else if( computer == 1 && human == 0){//컴승
				fCnt++;
			}else if( computer == 2 && human == 1){ //컴승
				fCnt++;
			}else{
				wCnt++;
				temp = "You WIN !!!!";
			}
		}
		
		gameCnt.setText(""+gCnt);
		winCnt.setText(""+wCnt);
		failCnt.setText(""+fCnt);
		safeCnt.setText(""+sCnt);

		computer = new Random().nextInt(3);

		message.append("\n" + temp + "\n\n");
		message.append("다음 게임을 시작하였습니다. 가위 바위 보중 하나를 선택하세요\n\n");
		message.setCaretPosition(message.getText().length());
	}
	
	
	private JTextField getSafeCnt() {
		if (safeCnt == null) {
			safeCnt = new JTextField();
			safeCnt.setText("0");
			safeCnt.setHorizontalAlignment(SwingConstants.CENTER);
			safeCnt.setBounds(181, 111, 65, 21);
			safeCnt.setColumns(10);
		}
		return safeCnt;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("무");
			lblNewLabel_5.setBounds(251, 112, 57, 15);
		}
		return lblNewLabel_5;
	}
}






